package com.junyidao.game.controller;

import com.junyidao.game.constant.BaseConstant;
import com.junyidao.game.constant.OperLogConstant;
import com.junyidao.game.core.LoginInfo;
import com.junyidao.game.core.annotation.Action;
import com.junyidao.game.pojo.PCode;
import com.junyidao.game.pojo.TChannel;
import com.junyidao.game.pojo.TGroup;
import com.junyidao.game.service.ChannelService;
import com.junyidao.game.service.CodeService;
import com.junyidao.game.service.UserService;
import com.junyidao.game.vo.GameVo;
import com.junyidao.game.vo.TChannelVo;
import com.junyidao.game.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * Created by zzh on 2016/7/7.
 */
@Controller
@RequestMapping("/channel")
public class ChannelController extends BaseController {

    @Autowired
    private ChannelService channelService;

    @Autowired
    private CodeService codeService;

    @Autowired
    private UserService userService;

    @RequestMapping
    @Action(desc = "获取管理")
    public ModelAndView list() {
        ModelAndView result = new ModelAndView("channel/list");
        List<TChannel> list = channelService.selectChannelBuUid(LoginInfo.getUser());
        result.addObject("channelList", list);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/list")
    @Action(desc = "获取渠道列表", sysPriv = false)
    public Map<String, Object> listData(TChannelVo channel) {
        Map<String, Object> map = channelService.selectChannel(channel);
        return resultSucess(map);
    }

    @RequestMapping(value = "/add")
    @Action(desc = "跳转到新增渠道页面", sysPriv = false)
    public ModelAndView add() {
        ModelAndView result = new ModelAndView("channel/add");
        List<PCode> plateformType = codeService.select("PlateformType");
        List<PCode> gameType = codeService.select("GameType");
        result.addObject("plateformType", plateformType);
        result.addObject("gameType", gameType);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/addChannel")
    @Action(operType = OperLogConstant.GROUP_ADD, desc = "新增渠道", needLog = true)
    public Map<String, Object> addChannel(TChannel channel) {
        Integer id = channelService.addChannel(channel);
        return resultSucess(id);
    }

    @ResponseBody
    @RequestMapping(value = "/activate")
    @Action(operType = OperLogConstant.GROUP_ACTIVATE, desc = "启用失效的渠道", needLog = true)
    public Map<String, Object> activate(String ids) {
        channelService.activate(ids, BaseConstant.STATUS_NORMAL);
        return resultSucess(null);
    }

    @ResponseBody
    @RequestMapping(value = "/disactivate")
    @Action(operType = OperLogConstant.GROUP_DISACTIVATE, desc = "注销启用的渠道", needLog = true)
    public Map<String, Object> disactivate(String ids) {
        channelService.activate(ids, BaseConstant.STATUS_DELETE);
        return resultSucess(null);
    }

    @ResponseBody
    @RequestMapping(value = "/listGame")
    @Action(desc = "获取游戏列表", sysPriv = false)
    public Map<String, Object> listGame(GameVo gameVo) {
        Map<String, Object> map = channelService.getGame(gameVo);
        return resultSucess(map);
    }

    @ResponseBody
    @RequestMapping(value = "/addGame")
    @Action(operType = OperLogConstant.CHANNEL_ADD_GAME, desc = "渠道--增加游戏", needLog = true)
    public Map<String, Object> addGame(String ids, Integer channelId) {
        channelService.addGame(ids, channelId);
        return resultSucess(null);
    }

    @ResponseBody
    @RequestMapping(value = "/delGame")
    @Action(operType = OperLogConstant.CHANNEL_DELETE_GAME, desc = "渠道--删除游戏", needLog = true)
    public Map<String, Object> delGame(String ids, Integer channelId) {
        channelService.delGame(ids, channelId);
        return resultSucess(null);
    }

    @RequestMapping(value = "/detail")
    @Action(desc = "群组详情", sysPriv = false)
    public ModelAndView detail(Integer channelId) {
        ModelAndView result = new ModelAndView("/channel/detail");
        List<PCode> plateformType = codeService.select("PlateformType");
        List<PCode> gameType = codeService.select("GameType");
        TChannel channel = channelService.selectChannelByChannelId(channelId);
        result.addObject("plateformType", plateformType);
        result.addObject("gameType", gameType);
        result.addObject("channel", channel);
        return result;
    }

    @RequestMapping(value = "/edit")
    @Action(desc = "群组详情", sysPriv = false)
    public ModelAndView edit(Integer channelId) {
        ModelAndView result = new ModelAndView("/channel/edit");
        List<PCode> plateformType = codeService.select("PlateformType");
        List<PCode> gameType = codeService.select("GameType");
        TChannel channel = channelService.selectChannelByChannelId(channelId);
        result.addObject("plateformType", plateformType);
        result.addObject("gameType", gameType);
        result.addObject("channel", channel);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/listUser")
    @Action(desc = "获取账号列表", sysPriv = false)
    public Map<String, Object> listUser(UserVo userVo) {
        Map<String, Object> map = userService.getUserByChannel(userVo);
        return resultSucess(map);
    }

    @ResponseBody
    @RequestMapping(value = "/update")
    @Action(operType = OperLogConstant.USER_EDIT, desc = "编辑群组", needLog = true)
    public Map<String, Object> update(TChannel channel) {
        channelService.updatePartByPrimaryKey(channel);
        return resultSucess(null);
    }

    @ResponseBody
    @RequestMapping(value = "/moveOutUser")
    @Action(operType = OperLogConstant.USER_EDIT, desc = "编辑群组", needLog = true)
    public Map<String, Object> moveOutUser(String cuid) {
        channelService.moveOutUser(cuid);
        return resultSucess(null);
    }
}
